﻿#include "AppMainWindow.h"
#include "ui_AppMainWindow.h"
//插件相关
#include "DAAbstractNodePlugin.h"
#include "DAAbstractPlugin.h"
#include "DAPluginManager.h"
#include "DAWorkFlowPluginManager.h"
//界面相关
#include "DAAppRibbonArea.h"
#include "DACore.h"
//对话框
#include "DAPluginManagerDialog.h"
//节点相关
#include "DAAbstractNodeFactory.h"
#include "DAAbstractNodeWidget.h"
#include "DANodeMetaData.h"

AppMainWindow::AppMainWindow(QWidget *parent) : SARibbonMainWindow(parent), ui(new Ui::AppMainWindow)
{
    ui->setupUi(this);
    //建立ribbonArea，此函数的构造函数会生成界面
    DACore::getInstance()->createAppRibbonArea(this);
    //首次调用此函数会加载插件，可放置在main函数中调用
    DAWorkFlowPluginManager::instance();
    m_workflow = new DADataWorkFlow(this);
    initUI();
    initDock();
    initConnect();
    showMaximized();
    setDefaultView();
}

AppMainWindow::~AppMainWindow()
{
    delete ui;
}

/**
 * @brief 隐藏所有dock
 */
void AppMainWindow::hideAllDockWidget()
{
    QList<QDockWidget *> dws = findChildren<QDockWidget *>();
    for (QDockWidget *w : dws) {
        removeDockWidget(w);
    }
}

void AppMainWindow::setupNodeListWidget()
{
    DAWorkFlowPluginManager &plugin = DAWorkFlowPluginManager::instance();
    QList<DAAbstractNodeFactory *> factorys = plugin.getNodeFactorys();
}

void AppMainWindow::initUI()
{
    //设置dock

    DAWorkFlowPluginManager &pluginmgr = DAWorkFlowPluginManager::instance();
    QList<DAAbstractNodeFactory *> factorys = pluginmgr.getNodeFactorys();
    //提取所有的元数据
    QList<DANodeMetaData> nodeMetaDatas;

    for (DAAbstractNodeFactory *factory : factorys) {
        nodeMetaDatas += factory->getNodesMetaData();
        //注册工厂
        m_workflow->registFactory(factory);
        qDebug() << "registFactory:" << factory->getPrototypes();
    }
    //对所有元数据按group进行筛分
    QMap<QString, QList<DANodeMetaData>> groupedNodeMetaDatas;

    for (const DANodeMetaData &md : nodeMetaDatas) {
        groupedNodeMetaDatas[md.getGroup()].append(md);
    }
    //把数据写入toolbox
    ui->toolBox->addItems(groupedNodeMetaDatas);
    ui->workFlowWidget->view()->setNodeFactory(m_workflow);
}

void AppMainWindow::initConnect()
{
    connect(ui->actionPluginManager, &QAction::triggered, this, &AppMainWindow::onActionPluginManagerTriggered);
    connect(ui->workFlowWidget->view(), &DAWorkFlowGraphicsView::selectNodeItemChanged, this, &AppMainWindow::onSelectNodeItemChanged);
}

void AppMainWindow::initDock()
{
    setDockNestingEnabled(true);
    setDockOptions(QMainWindow::AnimatedDocks | QMainWindow::AllowTabbedDocks | QMainWindow::AllowNestedDocks);
}

/**
 * @brief 设置为默认视图
 * @todo
 */
void AppMainWindow::setDefaultView()
{
    // TODO
    // splitDockWidget(ui->dockWidgetSetting, ui->dockWidgetInfomation);
}

void AppMainWindow::onActionPluginManagerTriggered(bool on)
{
    Q_UNUSED(on);
    DAPluginManagerDialog dlg(this);

    dlg.exec();
}

void AppMainWindow::onSelectNodeItemChanged(DAAbstractNodeGraphicsItem *i)
{
    if (nullptr == i) {
        return;
    }
    DAAbstractNodeWidget *w = i->getNodeWidget();
    if (nullptr == w) {
        return;
    }
    ui->dockWidgetSetting->setWidget(w);
}
